package com.example.administrator.newsapp.UIActivity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.administrator.newsapp.DialogManagers.AddNewsDialog;
import com.example.administrator.newsapp.Fragments.MVideoFragment;
import com.example.administrator.newsapp.Fragments.NewsFragment;
import com.example.administrator.newsapp.Fragments.PersonalFragment;
import com.example.administrator.newsapp.Fragments.VideoFragment;
import com.example.administrator.newsapp.R;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ArrayList<CustomTabEntity> mTabEntities=new ArrayList<>();
    private ArrayList<Fragment> mfragments=new ArrayList<>();
    private View mDecorView;
    private CommonTabLayout ctl;
    //声明tabentity的文字，选中和未选中的图标
    private String[] mTitles={"首页","西瓜视频"," ","小视频","我的"};
    private int[] mIconSelected={R.mipmap.home_selected,R.mipmap.video_selected,R.mipmap.add,R.mipmap.mvideo_selected,R.mipmap.personal_selected};
    private int[] mUnIconSelected={R.mipmap.hone_unselected,R.mipmap.video_unselected,R.mipmap.add,R.mipmap.mvideo_unselected,R.mipmap.personal_unselected};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             Window window = getWindow();
             window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
             window.setStatusBarColor(Color.TRANSPARENT);
         }
        setContentView(R.layout.activity_main);
        for (int i=0;i<mTitles.length;i++){
            mTabEntities.add(new TabEntity(mTitles[i],mIconSelected[i],mUnIconSelected[i]));
        }
        for (String title:mTitles){
            mfragments.add(new NewsFragment());
            mfragments.add(new VideoFragment());
            mfragments.add(new Fragment());
            mfragments.add(new MVideoFragment());
            mfragments.add(new PersonalFragment());
        }
        mDecorView=getWindow().getDecorView();
        ctl=(CommonTabLayout) findViewById(R.id.menu_list);
        ctl.setTabData(mTabEntities,this,R.id.fragment_summary,mfragments);
        ctl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                iconAnimation(position);
//                autoStatusBar(position);
            }


            @Override
            public void onTabReselect(int position) {

            }

        });
    }

    //发布头条按钮
    public void addNews(View view){
        AddNewsDialog addNewsDialog=new AddNewsDialog(this);
        addNewsDialog.showmenu();
    }

    //根据fragment的第一个view设置状态栏字体变化
    public void autoStatusBar(int i){
        Fragment fragment=mfragments.get(i);
        View view=fragment.getView().findViewById(R.id.topView);
        ColorDrawable cd=(ColorDrawable) view.getBackground();
        int color=cd.getColor();
        if (String.valueOf(color)=="-1"){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    //设置tablayout图标动画
    public void iconAnimation(int i){
        final ImageView imageView =ctl.getIconView(i);
        imageView.setScaleX(0.8f);
        imageView.setScaleY(0.8f);
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                imageView.setScaleX(1f);
                imageView.setScaleY(1f);
            }
        },100);
    }




}
